package com.mylibraryapplication;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.Snackbar;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.mylibrary.util.AbStrUtil;
import com.mylibrary.util.Tools;
import com.mylibrary.view.recyclerView.RecycleViewDivider;
import com.mylibrary.view.recyclerView.RecyclerOnItemClickListener;
import com.mylibrary.view.swipetoloadlayout.base.OnLoadMoreListener;
import com.mylibrary.view.swipetoloadlayout.base.OnRefreshListener;
import com.mylibrary.view.swipetoloadlayout.base.SwipeToLoadLayout;
import com.mylibraryapplication.activity.AlertDialogActivity;
import com.mylibraryapplication.activity.GuideActivity;
import com.mylibraryapplication.activity.ProgressBarActivity;
import com.mylibraryapplication.activity.RetrofitActivity;
import com.mylibraryapplication.activity.RxJavaActivity;
import com.mylibraryapplication.activity.TabIndicatorActivity;
import com.mylibraryapplication.activity.TimePickerActivity;
import com.mylibraryapplication.adapter.MainAdapter;
import com.mylibraryapplication.base.BaseActivity;
import com.mylibraryapplication.view.TitleView;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.jpush.android.api.JPushInterface;
import rx.Observable;
import rx.functions.Action1;

public class MainActivity extends BaseActivity {
    @Bind(R.id.refresh)
    SwipeToLoadLayout refresh;
    private String[] data = {"3D滚轮选择时间功能", "图片轮播", "启动页","手势密码","进度条","对话框","网络请求","RxJava"};
    private List<String> listData;
    @Bind(R.id.recycler_view)
    RecyclerView mainRecycler;
    private RelativeLayout rlLayout;
    private ImageView ivGuide;
    private MainAdapter mAdapter;
    private TitleView titleView;
    private Handler mHandler;
    private CoordinatorLayout coordinatorLayout;
    public static boolean isForeground = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initView();
        setOnClick();
        setRefresh();
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }

    protected void initView() {
        init();
        registerMessageReceiver();
        mHandler=new Handler();
        coordinatorLayout= (CoordinatorLayout) findViewById(R.id.layoutRoot);
        mainRecycler = (RecyclerView) findViewById(R.id.recycler_view);
        titleView = (TitleView) findViewById(R.id.title);
        refresh= (SwipeToLoadLayout) findViewById(R.id.refresh);
        rlLayout= (RelativeLayout) findViewById(R.id.main_recycler_rl);
        ivGuide= (ImageView) findViewById(R.id.main_iv);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                ivGuide.setVisibility(View.GONE);
            }
        },1000);

        setTitle(titleView, "首页");
        listData = new ArrayList<>();
        for (int i = 0; i < data.length; i++) {
            listData.add(data[i]);
        }

        LinearLayoutManager manager = new LinearLayoutManager(mContext);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        mainRecycler.setLayoutManager(manager);
//        mainRecycler.addItemDecoration(new RecycleViewDivider(mContext, RecycleViewDivider.VERTICAL_LIST));
        mAdapter = new MainAdapter(listData, this);
        mainRecycler.setAdapter(mAdapter);
//        refresh.setRefreshing(true);



    }
    // 初始化 JPush。如果已经初始化，但没有登录成功，则执行重新登录。
    private void init(){
        JPushInterface.init(getApplicationContext());
    }

    private void setOnClick() {
        mainRecycler.addOnItemTouchListener(new RecyclerOnItemClickListener(mainRecycler) {
            @Override
            public void OnItemClickListener(RecyclerView.ViewHolder viewHolder) {
                int position = viewHolder.getLayoutPosition();
                Snackbar.make(coordinatorLayout,"点击位置"+position,Snackbar.LENGTH_LONG)
                        .setActionTextColor(ContextCompat.getColor(mContext,R.color.app_main_color)).show();
                Intent intent = new Intent();
                if (position == 0) {
                    intent.setClass(mContext, TimePickerActivity.class);
                } else if (position == 1) {
                    intent.setClass(mContext, TabIndicatorActivity.class);
                } else if (position == 2) {
                    intent.setClass(mContext, GuideActivity.class);
                } else if(position==3){
                    Tools.showTextToast(mContext, position + "点击位置");
                    return;
                }else if(position==4) {
                    intent.setClass(mContext, ProgressBarActivity.class);
                }else if(position==5){
                    intent.setClass(mContext,AlertDialogActivity.class);
                }else if(position==6){
                    intent.setClass(mContext, RetrofitActivity.class);
                }else if(position==7){
                    intent.setClass(mContext, RxJavaActivity.class);
                }else{
                    Tools.showTextToast(mContext, position + "点击位置");
                    return;
                }
                startActivity(intent);
            }
        });
    }
    private void setRefresh(){
        refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        refresh.setRefreshing(false);
                        Snackbar.make(coordinatorLayout,"刷新完成",Snackbar.LENGTH_SHORT)
                                .setAction("click", new View.OnClickListener() {
                                    @Override
                                    public void onClick(View view) {
                                        Tools.showTextToast(mContext,"click");
                                    }
                                })
                                .setActionTextColor(ContextCompat.getColor(mContext,R.color.red_click_color)).show();
                    }
                },2000);

            }
        });
        refresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        refresh.setLoadingMore(false);
                        Snackbar.make(coordinatorLayout,"加载更多完成",Snackbar.LENGTH_SHORT)
                                .setAction("click", new View.OnClickListener() {
                                    @Override
                                    public void onClick(View view) {
                                        Tools.showTextToast(mContext,"click");
                                    }
                                })
                                .setActionTextColor(ContextCompat.getColor(mContext,R.color.red_click_color)).show();
                    }
                },2000);
            }
        });
    }


    //for receive customer msg from jpush server
    private MessageReceiver mMessageReceiver;
    public static final String MESSAGE_RECEIVED_ACTION = "com.example.jpushdemo.MESSAGE_RECEIVED_ACTION";
    public static final String KEY_TITLE = "title";
    public static final String KEY_MESSAGE = "message";
    public static final String KEY_EXTRAS = "extras";

    public void registerMessageReceiver() {
        mMessageReceiver = new MessageReceiver();
        IntentFilter filter = new IntentFilter();
        filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
        filter.addAction(MESSAGE_RECEIVED_ACTION);
        registerReceiver(mMessageReceiver, filter);
    }

    public class MessageReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (MESSAGE_RECEIVED_ACTION.equals(intent.getAction())) {
                String messge = intent.getStringExtra(KEY_MESSAGE);
                String extras = intent.getStringExtra(KEY_EXTRAS);
                StringBuilder showMsg = new StringBuilder();
                showMsg.append(KEY_MESSAGE + " : " + messge + "\n");
                if (!AbStrUtil.isEmpty(extras)) {
                    showMsg.append(KEY_EXTRAS + " : " + extras + "\n");
                }
                setCostomMsg(showMsg.toString());
            }
        }
    }
    String message;
    private void setCostomMsg(String msg){
        if (null != message) {
            message=msg;
            Log.e(TAG,"极光推送接收消息==="+message);
        }
    }

    @Override
    protected void onDestroy() {
        unregisterReceiver(mMessageReceiver);
        super.onDestroy();
    }
}
